import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;

public class ReceiveEmail {
    public static void main(String[] args) {
        // 配置邮件服务器属性
        Properties props = new Properties();
        props.put("mail.store.protocol", "imaps");
        props.put("mail.imaps.ssl.protocols", "TLSv1.2");
        props.put("mail.imaps.host", "imap.qq.com");
        props.put("mail.imaps.port", "993");
        
        // QQ 邮箱账户信息
        final String username = "1111@qq.com"; // QQ邮箱
        final String password = "1111"; //QQ邮箱密码
        
        try {
            // 创建会话
            Session session = Session.getDefaultInstance(props, null);
            Store store = session.getStore("imaps");
            store.connect("imap.qq.com", username, password);
            
            // 访问收件箱
            Folder inbox = store.getFolder("INBOX");
            inbox.open(Folder.READ_ONLY);
            
            // 获取邮件
            Message[] messages = inbox.getMessages();
            for (Message message : messages) {
                System.out.println("Subject: " + message.getSubject());
                System.out.println("From: " + message.getFrom()[0]);
                System.out.println("Text: " + message.getContent().toString());
            }
            
            inbox.close(false);
            store.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
